import { Toast } from "vant";
import VueRouter from "vue-router";

const router=new VueRouter({
    mode:'history',
    routes:[
        {
            path:'/',
            redirect:'/home',
        },
        {
            name:'home',
            path:'/home',
            component:()=>import('../components/view/HomeView.vue'),
            meta:{
                title:'首页'
            }
        },
        {
            name:'category',
            path:'/category',
            component:()=>import('../components/view/Category.vue'),
            meta:{
                title:'分类'
            }
        },
        {
            name:'shopCart',
            path:'/shopCart',
            component:()=>import('../components/view/ShopCart.vue'),
            meta:{
                title:'购物车'
            }
        },
        {
            name:'my',
            path:'/my',
            component:()=>import('../components/view/My.vue'),
            meta:{
                title:'我的'
            }
        },
        {
            name:'goodsList',
            path:'/goodsList',
            component:()=>import('../components/view/GoodsList.vue'),
            meta:{
                title:'商品列表'
            },
            props:true
        },
        {
            name:'goods',
            path:'/goods',
            component:()=>import('../components/view/Goods.vue'),
            meta:{
                title:'商品详情'
            },
            props:true
        },
        {
            name:'theme',
            path:'/theme',
            component:()=>import('../components/view/Theme.vue'),
            meta:{
                title:'专题详情'
            },
            props:true
        },
        {
            name:'categoryList',
            path:'/categoryList',
            component:()=>import('../components/view/CategoryList.vue'),
            meta:{
                title:'分类详情'
            },
            props:true
        },
        {
            name:'login',
            path:'/login',
            component:()=>import('../components/login/Login.vue'),
            meta:{
                title:'登录'
            },
        },
        {
            name:'buy',
            path:'/buy',
            component:()=>import('../components/view/Buy.vue'),
            meta:{
                title:'确认订单'
            },
            props:true
        },
    ]
})
router.beforeEach((to,from,next)=>{
    let needTokenPage=['buy','shopcart','order','my','adress']
    if(!needTokenPage.includes(to.name))return next();
    const token=localStorage.getItem('Authorization')
    if(!token){
        Toast.fail('请先登录')
        return next('/login')
    }
    next()
})
export default router;